<!DOCTYPE html>
<html  lang="en" xmlns:th="https://www.thymeleaf.org/">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/public/js/lay-module/formSelects/layui-v2.3.0-rc1/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/public/css/public.css}" media="all">
    <!--    <link rel="stylesheet" type="text/css" href="http://raw.githack.com/hnzzmsf/layui-formSelects/master/dist/formSelects-v4.css"/>-->
    <link rel="stylesheet" th:href="@{/public/js/lay-module/formSelects/formSelects-v4.css}" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="username" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入用户名"  th:value="${user.username}" class="layui-input">
            <tip>填写自己账号的名称</tip>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">真实姓名</label>
        <div class="layui-input-block">
            <input type="text" name="realName" th:value="${user.realName}" placeholder="请输入真实姓名"  class="layui-input"/>
            <tip>填写自己的真实姓名称</tip>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">手机</label>
        <div class="layui-input-block">
            <input type="number" name="phone" th:value="${user.phone}"  placeholder="请输入手机" value="" class="layui-input">
            <tip>填写自己的手机号码</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">邮箱</label>
        <div class="layui-input-block">
            <input type="email" name="email" th:value="${user.email}" placeholder="请输入邮箱" value="" class="layui-input">
            <tip>填写自己的电子邮箱</tip>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">权限</label>
        <div class="layui-input-block">
            <select name="roleIds" id="select1" xm-select="select1" lay-filter="select1">
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">部门</label>
        <div class="layui-input-block">
            <select name="deptIds" id="select2" xm-select="select2" lay-filter="select2">
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">用户状态</label>
        <div class="layui-input-block">
            <input type="radio" name="status" value="0" title="正常"  th:attr="checked=${user.status == 0}">
            <input type="radio" name="status" value="1" title="禁用"  th:attr="checked=${user.status == 1}">
        </div>
    </div>

    <input type="hidden" name="id" th:value="${user.id}">

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>

<script th:src="@{/public/js/lay-module/formSelects/layui-v2.3.0-rc1/layui/layui.js}" charset="utf-8"></script>
<script th:src="@{/public/js/lay-module/formSelects/formSelects-v4.js}" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;
        let formSelects = layui.formSelects;


        // 初始化下拉选择器
        $.get('/system/role/role_selects?value=' + '[[${user.id}]]', function (res) {
            let data = res.data;
            // 循环遍历data 将data的元素渲染到select1中
            formSelects.data('select1', 'local', {arr: data});
        });

        $.get('/system/dept/dept_selects?value=' + '[[${user.id}]]', function (res) {
            let data = res.data;
            // 循环遍历data 将data的元素渲染到select2中
            formSelects.data('select2', 'local', {arr: data});
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.post("/system/user/edit", data.field, function (res) {
                if (res.code === 200) {
                    layer.msg("更新用户成功!", {icon: 1, time: 1000}, function () {
                        let iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });
                } else {
                    layer.msg(res.msg, {icon: 2, time: 1000});
                }
            });

            return false;
        });

    });
</script>
</body>
</html>